home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / 342_01.zip / I8255TO1.C < prev    next >
C/C++ Source or Header  |  1993-04-03  |  4KB  |  131 lines

  1. /*-
  2.  *  ----------------------------------------------------------------------
  3.  *  File        :   I8255TO1.C
  4.  *  Creator     :   Blake Miller
  5.  *  Version     :   01.00.00            February 1991
  6.  *  Language    :   Microsoft Quick C   Version 2.0
  7.  *  Purpose     :   I8255 Digital IO
  8.  *              :   Demonstration Program #1
  9.  *  ----------------------------------------------------------------------
  10.  */
  11.  
  12. #include    <conio.h>
  13. #include    <stdio.h>
  14. #include    <stdlib.h>
  15. #include    "I8255FN.H"
  16.  
  17. #define     PPI_BASE    0x300   /* I8255 Base Address   */
  18.  
  19. void    main ( void );
  20. void kb_dump ( void );  /* flush keyboard buffer    */
  21. int  kb_wait ( void );  /* wait for keypress        */
  22.  
  23. /*  convert unsigned data value to a binary string
  24.  */
  25. extern void scv_uvtobs ( void *, char *, int, int, int, int );
  26.  
  27. void main (void)
  28.     {
  29.     unsigned char   inbyte;         /* input byte   */
  30.     I8255DAT        diodat;         /* 8255 data    */
  31.     char            strdat[16];     /* binary text  */
  32.  
  33.     /*  Initialize 8255 data, assuming base address is at PPI_BASE
  34.      */
  35.     I8255_init (&diodat, PPI_BASE);
  36.  
  37.     /*  Initialize 8255 hardware, and set all ports to output
  38.      */
  39.     I8255_config (&diodat, 0, 0, 0, 0);
  40.  
  41.     /*  Set data for all ports to zero
  42.      */
  43.     I8255_put_byte (&diodat, 1, 0);
  44.     I8255_put_byte (&diodat, 2, 0);
  45.     I8255_put_byte (&diodat, 3, 0);
  46.  
  47.     printf ("Set port A to all 1\n");
  48.     I8255_put_byte (&diodat, 1, 0xFF);
  49.     kb_wait();
  50.     printf ("Set port A to all 0\n");
  51.     printf ("Set port B to all 1\n");
  52.     I8255_put_byte (&diodat, 1, 0x00);
  53.     I8255_put_byte (&diodat, 2, 0xFF);
  54.     kb_wait();
  55.     printf ("Set port B to all 0\n");
  56.     printf ("Set port C to all 1\n");
  57.     I8255_put_byte (&diodat, 2, 0x00);
  58.     I8255_put_byte (&diodat, 3, 0xFF);
  59.     kb_wait();
  60.     I8255_put_byte (&diodat, 3, 0x00);
  61.  
  62.     printf ("Set all ports to input.\n");
  63.     I8255_config (&diodat, 1, 1, 1, 1);
  64.  
  65.     kb_dump ();
  66.     printf ("\nPrint out input value of Port A till keypress...\n");
  67.     while ( !kbhit() ){
  68.         I8255_get_byte (&diodat, 1, &inbyte);
  69.         scv_uvtobs ( (void *)&inbyte, strdat, 7, 0, '1', '0' );
  70.         printf ("Port A Data : %02X = %s\r", (unsigned int)inbyte, strdat);
  71.         }
  72.  
  73.     kb_dump ();
  74.     printf ("\nPrint out input value of Port B till keypress...\n");
  75.     while ( !kbhit() ){
  76.         I8255_get_byte (&diodat, 2, &inbyte);
  77.         scv_uvtobs ( (void *)&inbyte, strdat, 7, 0, '1', '0' );
  78.         printf ("Port B Data : %02X = %s\r", (unsigned int)inbyte, strdat);
  79.         }
  80.  
  81.     kb_dump ();
  82.     printf ("\nPrint out input value of Port C till keypress...\n");
  83.     while ( !kbhit() ){
  84.         I8255_get_byte (&diodat, 3, &inbyte);
  85.         scv_uvtobs ( (void *)&inbyte, strdat, 7, 0, '1', '0' );
  86.         printf ("Port C Data : %02X = %s\r", (unsigned int)inbyte, strdat);
  87.         }
  88.  
  89.     exit (0);
  90.     }
  91.  
  92. /*- Flush Keyboard ----------------------------**
  93.  *  Check for keypress, and read them out of
  94.  *  the buffer until there are no more.
  95.  */
  96. void kb_dump (void)
  97.     {
  98.     short   ch;
  99.     while ( kbhit() ){          /* key was pressed          */
  100.         ch = getch();           /* read out keypress        */
  101.         if ( (ch == 0x00) || (ch == 0xE0) )
  102.             getch();            /* eat function key codes   */
  103.         }
  104.     }
  105.  
  106. /*- Keyboard Wait ----------------------------**
  107.  *  Flush keyboard.
  108.  *  Print blank line, then print a message to press
  109.  *  spacebar to continue, then wait for keypress, then
  110.  *  erase message and return.
  111.  */
  112. int kb_wait ( void )
  113.     {
  114.     short   ch;
  115.  
  116.     kb_dump ();
  117.     printf ("\r\n");
  118.     printf ("[ PRESS <SPACE-BAR> TO CONTINUE ]\r");
  119.     ch = getch();
  120.     if ( (ch == 0x00) || (ch == 0xE0) )
  121.         ch = getch() | 0x0100;
  122.     printf ("                                 \r");
  123.     return (ch );
  124.     }
  125.  
  126. /*-
  127.  *  ----------------------------------------------------------------------
  128.  *  END I8255T01.C Source File
  129.  *  ----------------------------------------------------------------------
  130.  */
  131.